'use strict';
app.controller('setMealCtrl',['$scope','utilService','$modal', function($scope,utilService,$modal) {
    $scope.setMeal = {};
    // call server.
    $scope.query = function () {
        utilService.queryForPagingList($scope, {
            url: $scope.app.domain + 'set/meal',
            params: $scope.setMeal
        }).then(function (result) {
        });
    }
    $scope.query();


    $scope.openDelete = function (id,name) {
        var modalInstance = $modal.open({
            templateUrl: 'deleteSetMeal.html',
            controller: 'deleteSetMealCtrl',
            size:'sm',
            scope:$scope,
            resolve: {
                data:function(){
                    var data={};
                    data.id=id;
                    data.name=name;
                    return data;
                }
            }
        });

        modalInstance.result.then(function (result) {
            console.log(result);
        }, function () {
            console.info('Modal dismissed at: ' + new Date());
        });

    };

}]);


app.controller('setMealAddCtrl',['$scope','setMealService','$stateParams','toaster','$state',
    function($scope,setMealService,$stateParams,toaster,$state) {
        $scope.setMeal={type:0,isOpenFree:0}

        $scope.addSetMeal = function(){
            setMealService.setMealAdd($scope).then(function(result){
                switch (result.status){
                    case 200:
                        toaster.pop('success', "提示", "操作成功");
                        $state.go("app.setMeal");
                        break;
                    case 604:
                        toaster.pop('error', "操作失败", "行业名称已存在");
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break
                }
            });
        }
}]);

app.controller('setMealViewCtrl',['$scope','setMealService','$stateParams','toaster','$state',
    function($scope,setMealService,$stateParams,toaster,$state) {
        $scope.id = $stateParams.id;
        $scope.findById=function(){
            setMealService.findById($scope).then(function(result){
                switch (result.status){
                    case 200:
                        $scope.setMeal = result.data;
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break
                }

            });
        }

        $scope.findById();

        $scope.editSetMeal = function(){
            setMealService.updateSetMeal($scope).then(function(result){
                switch (result.status){
                    case 200:
                        toaster.pop('success', "提示", "操作成功");
                        $state.go("app.setMeal");
                        break;
                    case 604:
                        toaster.pop('error', "操作失败", "行业名称已存在");
                        break;
                    default :
                        toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                        break
                }
            });
        }
}]);


//删除模态框Controller
app.controller('deleteSetMealCtrl', ['$scope', '$modalInstance','$state','setMealService','toaster','data',
                            function($scope, $modalInstance,$state,setMealService,toaster,data) {
    $scope.data=data;

    $scope.AllowToDelete=true;

    //确认删除
    $scope.ok=function(){
        if(!$scope.AllowToDelete){
            return;
        }

        $scope.AllowToDelete=false;

        var id=$scope.data.id;
        setMealService.deleteSetMeal($scope,id).then(function(result){
            switch (result.status){
                case 200:
                    $modalInstance.dismiss('success');
                    toaster.pop('success', "提示", "操作成功");
                    $state.reload('app.goodsType');
                    $scope.AllowToDelete=true;
                    break;
                case 607:
                    toaster.pop('waiting', "提示", result.message);
                    $scope.AllowToDelete=true;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    //取消弹窗
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };

}])
;